#ifndef SERVERTHREADS_H
#define SERVERTHREADS_H

#include <QTcpServer>
#include <QList>
#include "ClientSocket.h"

#include <QThread>

/*
 * 这个和TcpServer用法类似,每个socket一个线程
 * */


//template<class _TcpSocket_>
class ClientSocketThread : public QThread   //这个类类似于TcpSocket
{
    Q_OBJECT
public:
    ClientSocketThread(int socketDescriptor, QObject *parent);
    ~ClientSocketThread();

protected:
    virtual void run();

signals:
    void socketDisconnected(ClientSocketThread *pThread);

signals:
//以下分别是TcpSocket发出的信号,Thread作为中间人
    void hostFound(TcpSocket *);
    void connected(TcpSocket *);
    void disconnected(TcpSocket *);
    void stateChanged(qint16,TcpSocket *);
    void error(qint16,TcpSocket *);

    void readyRead(TcpSocket *);
    void bytesWritten(qint64 bytes,TcpSocket *);
    void aboutToClose(TcpSocket *);
    void readChannelFinished(TcpSocket *);
private:
    int socketDescriptor;
public:
    TcpSocket *pTcpSocket;
};





class TcpServerThread : public QTcpServer
{
    Q_OBJECT
public:
    TcpServerThread(QObject * parent = 0);
    ~TcpServerThread();

protected:

    //如果要使用派生ClientSocket时,必须重载
    virtual ClientSocketThread* createSocketThread(qintptr handle);

    //连接客户端方法2:手动创建QTcpSocket
    //virtual void incomingConnection(int socketId)
    virtual void incomingConnection(qintptr handle);

signals:
    void socketDisconnected(ClientSocketThread *pThread);

signals:
//以下分别是TcpSocket发出的信号,Server作为中间人
    void hostFound(TcpSocket *pTcpSocket);
    void connected(TcpSocket *pTcpSocket);
    void disconnected(TcpSocket *pTcpSocket);
    void stateChanged(qint16,TcpSocket *pTcpSocket);
    void error(qint16,TcpSocket *pTcpSocket);

    void readyRead(TcpSocket *pTcpSocket);
    void bytesWritten(qint64 bytes,TcpSocket *pTcpSocket);
    void aboutToClose(TcpSocket *pTcpSocket);
    void readChannelFinished(TcpSocket *pTcpSocket);

public slots:
    void Remove(ClientSocketThread *pThread);

public:
    QList<ClientSocketThread*> clientList;
};


#endif // SERVERTHREADS_H
